#include "mainscene.h"
#include "ui_mainscene.h"
#include"map.h"
#include"apple.h"
#include"bomb.h"
#include"diamond.h"
#include<QKeyEvent>
#include<qtimer.h>
#include<qrect.h>
#include<qpainter.h>
#include<newwindow.h>
#include <ctime>
#include<QFont>
#include <QPushButton>
#include<QSize>
#include<QApplication>


MainScene::MainScene(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainScene),blsRun(false)
{

      QPushButton * btn = new QPushButton("", this);
       //移动btn
       btn->move(10, 50);
       btn->resize(60,40);
       btn->setIcon(QIcon(":/myImage/images/play.png"));
       btn->setIconSize(QSize(42,42));
       btn->setStyleSheet("background-color:pink");

       connect(btn, SIGNAL(clicked(bool)), this, SLOT(CLICK()));

       QPushButton * btn1 = new QPushButton("", this);
        //移动btn1
        btn1->move(10, 90);
        btn1->resize(60,40);
        btn1->setIcon(QIcon(":/myImage/images/replay.png"));
        btn1->setIconSize(QSize(55,55));
        btn1->setStyleSheet("background-color:pink");
        connect(btn1, SIGNAL(clicked(bool)), this, SLOT(CLICK1()));

        QPushButton * btn2 = new QPushButton("", this);
        //移动btn2
        btn2->move(10, 130);
        btn2->resize(60,40);
        btn2->setIcon(QIcon(":/myImage/images/pause.png"));
        btn2->setIconSize(QSize(50,50));
        btn2->setStyleSheet("background-color:pink");
        connect(btn2, SIGNAL(clicked(bool)), this, SLOT(CLICK2()));

        QPushButton * btn3 = new QPushButton("", this);
        //移动btn2
        btn3->move(10, 170);
        btn3->resize(60,40);
        btn3->setIcon(QIcon(":/myImage/images/goon.png"));
        btn3->setIconSize(QSize(45,45));
        btn3->setStyleSheet("background-color:pink");
        connect(btn3, SIGNAL(clicked(bool)), this, SLOT(CLICK3()));



     ui->setupUi(this);
     this->setGeometry(QRect(400,100,512,768));

    //初始化玩家飞机速度
    plane_speed=20;

}

MainScene::~MainScene()
{
    delete ui;
}



void MainScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    if(blsRun==false)
        initScene();

    //绘制地图
    painter.drawPixmap(0,m_map.m_map1_posY , m_map.m_map1);


    //绘制飞机
    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);


    //绘制苹果
    for(int i = 0 ; i< 20;i++)
    {
      if(m_apples[i].m_Free == false)
        {
            painter.drawPixmap(m_apples[i].m_X,m_apples[i].m_Y,m_apples[i].m_apple);
        }
    }

    //绘制炸弹
    for(int i = 0 ; i< 5;i++)
    {
      if(m_bombs[i].m_Free == false)
        {
            painter.drawPixmap(m_bombs[i].m_X,m_bombs[i].m_Y,m_bombs[i].m_bomb);
        }
    }

    //绘制钻石
    for(int i = 0 ; i< 3;i++)
    {
      if(m_diamonds[i].m_Free == false)
        {
            painter.drawPixmap(m_diamonds[i].m_X,m_diamonds[i].m_Y,m_diamonds[i].m_diamond);
        }
    }

    //绘制得分
    QFont font1("Courier",20);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(40,25,scorelabel);
    painter.drawText(90,25,QString::number(score));

    //绘制时间
    QFont font2("Courier",20);
    painter.setFont(font2);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    painter.drawText(160,25,scorelabel1);
    painter.drawText(350,25,QString::number(remain_time));
    painter.drawText(390,25,scorelabel2);

    //绘制开始结束
    QFont font3("Courier",40);
    painter.setFont(font3);
    painter.setPen(Qt::yellow);
    painter.setBrush(Qt::yellow);
    painter.drawText(150,350,sDisplay);

    //绘制加速
    QFont font4("Courier",30);
    painter.setFont(font4);
    painter.setPen(Qt::green);
    painter.setBrush(Qt::green);
    painter.drawText(150,350,speed_up);


}

void MainScene::keyPressEvent(QKeyEvent *event)
{
    QKeyEvent *key=(QKeyEvent*)event;
    switch (key->key())
    {
    case Qt::Key_Left:ndirection=1;//向左
        break;
    case Qt::Key_Right:ndirection=2;//向右
        break;
    default:;
    }
}

void MainScene::CLICK()
{
    initScene();
    timer=new QTimer(this);
    timer->start(plane_speed);
    connect(timer,SIGNAL(timeout()),SLOT(plane_update()));

    timer1=new QTimer(this);
    timer1->start(20);//APPLE_RATE=20
    connect(timer1,SIGNAL(timeout()),SLOT(appleToScene()));
    connect(timer1,SIGNAL(timeout()),SLOT(newapple()));
    connect(timer1,SIGNAL(timeout()),SLOT(ifeat()));

    connect(timer1,SIGNAL(timeout()),SLOT(bombToScene()));
    connect(timer1,SIGNAL(timeout()),SLOT(newbomb()));
    connect(timer1,SIGNAL(timeout()),SLOT(ifbomb()));
    connect(timer1,SIGNAL(timeout()),SLOT(diamondToScene()));
    connect(timer1,SIGNAL(timeout()),SLOT(newdiamond()));
    connect(timer1,SIGNAL(timeout()),SLOT(ifdiamond()));

    timer2=new QTimer(this);
    timer2->start(1000);
    connect(timer2,SIGNAL(timeout()),SLOT(time_record()));
}

void MainScene::CLICK1()
{

    //初始化玩家飞机速度
        plane_speed=20;
        remain_time=0;
        remain_time2=0;
    //初始化苹果炸弹速度
        for(int i=0;i<20;i++)
        {
            m_apples[i].m_Speed=5;
        }

        for(int i=0;i<5;i++)
        {
            m_bombs[i].m_Speed=5;
        }
        for(int i=0;i<3;i++)
        {
            m_diamonds[i].m_Speed=5;
        }


        MainScene::CLICK();

}

void MainScene::CLICK2()
{
    timer->stop();
    timer1->stop();
    timer2->stop();
}
void MainScene::CLICK3()
{
    timer->start();
    timer1->start();
    timer2->start();
}
void MainScene::initScene()
{

    //设置窗口标题

    setWindowTitle("对小学生可能有些幼稚，但对大学生刚刚好 final");

    //飞机计时器设置
    ndirection=2;//默认向右运动
    blsRun=true;

    //初始化苹果出场间隔记录
    m_recorder = 0;
    //初始化炸弹出场间隔记录
    m_recorder1 = 0;


    //显示
    score=0;
    remain_time=0;
    sDisplay="游戏开始😼";

    //随机数种子
        srand((unsigned int)time(NULL));




//    timer=new QTimer(this);
//    timer->start(plane_speed);
//    connect(timer,SIGNAL(timeout()),SLOT(plane_update()));

//    timer1=new QTimer(this);
//    timer1->start(20);//APPLE_RATE=20
//    connect(timer1,SIGNAL(timeout()),SLOT(appleToScene()));
//    connect(timer1,SIGNAL(timeout()),SLOT(newapple()));
//    connect(timer1,SIGNAL(timeout()),SLOT(ifeat()));

//    connect(timer1,SIGNAL(timeout()),SLOT(bombToScene()));
//    connect(timer1,SIGNAL(timeout()),SLOT(newbomb()));
//    connect(timer1,SIGNAL(timeout()),SLOT(ifbomb()));

//    timer2=new QTimer(this);
//    timer2->start(1000);
//    connect(timer2,SIGNAL(timeout()),SLOT(time_record()));

}

void MainScene::time_record()
{
    sDisplay="";
    remain_time++;
    remain_time2++;

    speed_up="";

    //每15s加速2，人物移动计时器减去4

    if(remain_time2%15==0)
    {
        for(int i=0;i<20;i++)
        {
            m_apples[i].m_Speed+=2;
        }

        for(int i=0;i<5;i++)
        {
            m_bombs[i].m_Speed+=2;
        }

        for(int i=0;i<3;i++)
        {
            m_diamonds[i].m_Speed+=2;
        }

        timer->stop();
        plane_speed-=4;
        timer->start(plane_speed);
        speed_up="SPEED UP!💃";

    }
    update();
}




void MainScene::plane_update()
{
    int x = m_hero.m_X; //位置 - 飞机矩形的一半
    int y = m_hero.m_Y;
    switch(ndirection)
    {
    case(1):x-=5;
        break;
    case(2):x+=5;
        break;
    default:;
    }
    //边界检测
    if(x <= 0 )
    {
        x = 0;
    }
    if(x >= 512 - m_hero.m_Rect.width())
    {
        x = 512 - m_hero.m_Rect.width();
    }
    if(y <= 0)
    {
        y = 0;
    }
    if(y >= 768 - m_hero.m_Rect.height())
    {
        y = 768 - m_hero.m_Rect.height();
    }
    m_hero.setPosition(x,y);

    update();

}

void MainScene::appleToScene()
{

    //苹果的坐标更新

    for(int i = 0 ; i< 20;i++)
        {
            //非空闲苹果 更新坐标
           if(m_apples[i].m_Free == false)
           {
              m_apples[i].updatePosition();
           }
        }

    update();
}

void MainScene::newapple()
{
    m_recorder++;
    if(m_recorder < 40)  //苹果出场间隔为40
    {
        return;
    }

    m_recorder = 0;

    for(int i = 0 ; i< 20;i++)   //20个苹果
    {
        if(m_apples[i].m_Free)
        {
            //苹果空闲状态改为false
            m_apples[i].m_Free = false;
            //设置坐标
            m_apples[i].m_X = rand() % (512 - m_apples[i].m_Rect.width());
            m_apples[i].m_Y = -m_apples[i].m_Rect.height();
            break;
        }
    }
    update();
}


void MainScene::ifeat()
{

    for(int j = 0 ; j < 20;j++)
            {

                if(m_apples[j].m_Free)
                {
                    //空闲苹果 跳转下一次循环
                    continue;
                }

                //如果苹果和飞机相交，苹果空闲
                if((m_hero.m_X<m_apples[j].m_X&&m_hero.m_X+m_hero.m_Rect.width()>m_apples[j].m_X)
                   &&(m_hero.m_Y<m_apples[j].m_Y&&m_hero.m_Y+m_hero.m_Rect.height()>m_apples[j].m_Y))
                {

                    if(m_apples[j].m_Free==false)
                    {
                        score+=1;

                    }
                     m_apples[j].m_Free = true;
                }
            }
    update();
}



void MainScene::bombToScene()
{

    //炸弹的坐标更新

    for(int i = 0 ; i< 5;i++)
        {

            //非空闲炸弹 更新坐标
           if(m_bombs[i].m_Free == false)
           {
              m_bombs[i].updatePosition();
           }
        }

    update();
}

void MainScene::newbomb()
{

    m_recorder1++;
    if(m_recorder1 < 150)  //炸弹出场间隔为150
    {
        return;
    }

    m_recorder1 = 0;

    for(int i = 0 ; i< 5;i++)   //5个炸弹
    {
        if(m_bombs[i].m_Free)
        {
            //炸弹空闲状态改为false
            m_bombs[i].m_Free = false;
            //设置坐标
            m_bombs[i].m_X = rand() % (512 - m_bombs[i].m_Rect.width());
            m_bombs[i].m_Y = -m_bombs[i].m_Rect.height();
            break;
        }
    }
    update();
}


void MainScene::ifbomb()
{

    for(int j = 0 ; j < 5;j++)
            {

                if(m_bombs[j].m_Free)
                {
                    //空闲炸弹 跳转下一次循环
                    continue;
                }

                //如果炸弹和飞机相交，炸弹空闲
                if((m_hero.m_X<m_bombs[j].m_X&&m_hero.m_X+m_hero.m_Rect.width()>m_bombs[j].m_X)
                   &&(m_hero.m_Y<m_bombs[j].m_Y&&m_hero.m_Y+m_hero.m_Rect.height()>m_bombs[j].m_Y))
                {

                    if(m_bombs[j].m_Free==false)
                    {
                        timer->stop();
                        timer1->stop();
                        timer2->stop();
                        sDisplay="游戏结束😿";

                    }
                     m_bombs[j].m_Free = true;
                }
            }
    update();
}

void MainScene::diamondToScene()
{

    //炸弹的坐标更新

    for(int i = 0 ; i< 3;i++)
        {

            //非空闲炸弹 更新坐标
           if(m_diamonds[i].m_Free == false)
           {
              m_diamonds[i].updatePosition();
           }
        }

    update();
}

void MainScene::newdiamond()
{

    m_recorder2++;
    if(m_recorder2 < 180)  //炸弹出场间隔为150
    {
        return;
    }

    m_recorder2 = 0;

    for(int i = 0 ; i< 3;i++)   //5个炸弹
    {
        if(m_diamonds[i].m_Free)
        {
            //炸弹空闲状态改为false
            m_diamonds[i].m_Free = false;
            //设置坐标
            m_diamonds[i].m_X = rand() % (512 - m_diamonds[i].m_Rect.width());
            m_diamonds[i].m_Y = -m_diamonds[i].m_Rect.height();
            break;
        }
    }
    update();
}


void MainScene::ifdiamond()
{


    for(int j = 0 ; j < 3;j++)
            {

                if(m_diamonds[j].m_Free)
                {
                    //空闲炸弹 跳转下一次循环
                    continue;
                }

                //如果炸弹和飞机相交，炸弹空闲
                if((m_hero.m_X<m_diamonds[j].m_X&&m_hero.m_X+m_hero.m_Rect.width()>m_diamonds[j].m_X)
                   &&(m_hero.m_Y<m_diamonds[j].m_Y&&m_hero.m_Y+m_hero.m_Rect.height()>m_diamonds[j].m_Y))
                {

                    if(m_diamonds[j].m_Free==false)
                    {

                        remain_time+=10;
                    }
                     m_diamonds[j].m_Free = true;
                }
            }
    update();
}


void MainScene::on_pushButton_clicked()
{
    newWindow *configWindow= new newWindow;
    configWindow->show();
}

